package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author liuyandeng
 * @date 2023/8/11
 * @Description
 */

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/myForward") // localhost:8080/test/myForward
    public String myForward() {
        return "forward:/test.html";
    }

    @RequestMapping("/myRedirect") // localhost:8080/test/myRedirect
    public String myRedirect() {
        return "redirect:/test.html";
    }

    @RequestMapping("/login")
    @ResponseBody
    public HashMap<String, Object> login(@RequestBody User user) {
        HashMap<String, Object> hashMap = new HashMap<>();
        String msg = "用户名或者密码登录失败";
        int result = 0;
        if (StringUtils.hasLength(user.getName()) && StringUtils.hasLength(user.getPassword())
                && user.getName().equals("admin") && user.getPassword().equals("admin")) {
            result = 1;
            msg = "";
        }
        hashMap.put("succ", 200);
        hashMap.put("result", 1);
        hashMap.put("msg", msg);
        return hashMap;
    }

    @RequestMapping("/json") // localhost:8080/json
    @ResponseBody
    public HashMap<String, String> json() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", "java");
        hashMap.put("password", "123");
        return hashMap;
    }

    @RequestMapping("/calc") //localhost:8080/calc
    @ResponseBody
    public String calc(Integer num1, Integer num2) {
        return "计算结果：" + (num1 + num2);
    }

    @RequestMapping("/index") // localhost:8080/index
    public String getIndex() {
        return "index.html";
    }
}
